const Router = require('koa-router');
const eventCtl= require('../../controllers/events.js')
const router = new Router();

/**
 * @swagger
 * tags:
 *   - name: event
 *     description: Everything about event
 */

router
/**
 * @swagger
 * paths:
 *   /events:
 *     get:
 *       tags:
 *         - event
 *       summary: Return a list of events
 *       description: ''
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 *           schema:
 *             type: array
 *             items:
 *               $ref: '#/definitions/event'
 */
    .get('/', async (ctx, next)=>{ ctx.body = await eventCtl.list(); })

/**
 * @swagger
 * paths:
 *   /events:
 *     post:
 *       tags:
 *         - event
 *       summary: Create a event
 *       description: ''
 *       parameters:
 *         - in: body
 *           name: "body"
 *           description: ""
 *           required: true
 *           schema:
 *             $ref: "#/definitions/event"
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .post('/', async (ctx, next)=>{
        const {id} = ctx.params;
        const data = ctx.request.body;
        let  r = await eventCtl.create(data);
        ctx.body = r;
    })
/**
 * @swagger
 * paths:
 *   /events/{id}:
 *     get:
 *       tags:
 *         - event
 *       summary: Return a kind of events
 *       description: ''
 *       parameters:
 *         - in: path
 *           name: id 
 *           description: ""
 *           required: true
 *           type: string
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 *           schema:
 *             $ref: '#/definitions/event'
 */
    .get('/:id', async (ctx, next)=>{
        // Retrieves informations about a given event
        const {id} = ctx.params;
        const r = await eventCtl.retrieve(id);
        ctx.assert(r !== null, 404, 'Detail not found!');

        ctx.body = r;
    })

    .get('/:id/e', async (ctx, next)=>{
        // Retrieves informations about a given event of the given match
        const {id} = ctx.params;
        const {match_id, playerId} = ctx.query;
        const r = await eventCtl.find({result_id: id, match_id, playerId});
        //ctx.assert(r !== null, 404, 'Detail not found!');

        ctx.body = r;
    })

/**
 * @swagger
 * paths:
 *   /events/{id}:
 *     put:
 *       tags:
 *         - event
 *       summary: Update or create a event
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: id 
 *           description: ""
 *           required: true
 *           type: string
 *         - in: body
 *           name: body
 *           description: ""
 *           required: true
 *           schema:
 *             $ref: "#/definitions/event"
 *
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .put('/:id', async (ctx, next)=>{
        const id = ctx.params.id;
        const data = ctx.request.body;
        const r = await eventCtl.upsert(id, data);
        
        ctx.body = r;
    })

/**
 * @swagger
 * paths:
 *   /events/{id}:
 *     delete:
 *       tags:
 *         - event
 *       summary: Delete a event
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: id 
 *           description: ""
 *           required: true
 *           type: string
 *
 *       produces:
 *         - application/json
 *       responses:
 *         204:
 *           description: No content
 */
    .delete('/:id', async (ctx, next)=>{
        eventCtl.removeById(ctx.params.id);
        ctx.status = 204;
    })
/**
 * @swagger
 * paths:
 *   /competitions/{cid}/seasons:
 *     get:
 *       tags:
 *         - competition
 *       summary: Return the list of seasons of the given competition
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: cid 
 *           description: ""
 *           required: true
 *           type: string
 *
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .get('/:id/seasons', async (ctx, next)=>{
        const {id} = ctx.params;
        ctx.body = "TODO find the list of seasons of competition" + id;
        ctx.throw(500);
    })

module.exports = router;

/**
 * @swagger
 * resourcePath: /api
 * description: All about API
 * definitions:
 *   event:
 *     type: "object"
 *     required:
 *       - "name"
 *     properties:
 *       eventId:
 *         type: "number"
 *       eventName:
 *         type: "string"
 *       id:
 *         type: "number"
 *       matchId:
 *         type: "string"
 *       playerId:
 *         type: "string"
 *       subEventId:
 *         type: "number"
 *       subEventName:
 *         type: "string"
 *       teamId:
 *         type: "string"
 *       eventResults:
 *         type: "string"
 *       passFrom:
 *         type: "string"
 *       passTo:
 *         type: "string"
 *       startX:
 *         type: "number"
 *       startY:
 *         type: "number"
 *       endX:
 *         type: "number"
 *       endY:
 *         type: "number"
 */